package org.colafries.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.colafries.admin.entity.vo.ColafriesBaseVO;
import org.colafries.admin.service.IBaseService;

import java.util.List;

/**
 * @author Ann
 * @version v1.0
 * @ClassName BaseServiceImpl
 * @Description
 * @date Created in 2024/6/7
 **/
public class IBaseServiceImpl<M extends BaseMapper<T>, T, V extends ColafriesBaseVO> extends ServiceImpl<M, T> implements IBaseService<T, V> {

    /**
     * 查询所有listAll
     *
     * @param v
     * @return java.util.List<T>
     * @Author anning
     * @Date 2024/6/11
     */
    @Override
    public List<T> listAll(V v) {
        LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 新增
     *
     * @param v
     * @return int
     * @Author anning
     * @Date 2024/6/11
     */
    @Override
    public int add(V v) {
        return 0;
    }

    /**
     * 修改
     *
     * @param v
     * @return int
     * @Author anning
     * @Date 2024/6/11
     */
    @Override
    public int update(V v) {
        return 0;
    }

    /**
     * 删除
     *
     * @param v
     * @return int
     * @Author anning
     * @Date 2024/6/11
     */
    @Override
    public int delete(V v) {
        return 0;
    }

    /**
     * 分页查询
     *
     * @param v
     * @return com.baomidou.mybatisplus.core.metadata.IPage<T>
     * @Author anning
     * @Date 2024/6/11
     */
    @Override
    public IPage<T> pageList(V v) {
        LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 其他筛选条件 ... ...
        Page<T> page = new Page<>(v.getCurrent(), v.getSize());
        IPage<T> result = baseMapper.selectPage(page, queryWrapper);

        if (result.getTotal() > 0 && result.getRecords().isEmpty()) {
            long current = result.getPages();
            page = new Page<>((int) current, v.getSize());
            result = baseMapper.selectPage(page, queryWrapper);
        }
        return result;
    }
}
